package me.zhengjie.config.rabbit.istener;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import me.zhengjie.config.mica.mqtt.listener.MqttConnectStatusListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.service.MagicAPIService;

import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitMessageListener {
    private static final Logger logger = LoggerFactory.getLogger(RabbitMessageListener.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    MagicAPIService service;


    @Bean
    public Queue huidaGps(){
        return new Queue("huida.gps");
    }
    @Bean
    public Queue huidaEcu(){
        return new Queue("huida.ecu");
    }
    @Bean
    public Queue huidaWorkarea(){
        return new Queue("huida.workarea");
    }
    @Bean
    public Queue huidaOnoff(){
        return new Queue("huida.onoff");
    }
    @Bean
    public Queue huidaTerminal(){
        return new Queue("huida.terminal");
    }
    @Bean
    public Queue sunongComon(){
        return new Queue("sunong.common");
    }


    @RabbitListener(queues = {"huida.gps"})
    public void listenerHuidaGpsMessage(@Payload String message,
                                          @Headers Map<String,Object> headers,
                                          @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey
    ){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/mq_huida_gps", params);
        logger.info("huida.gps callback result:{} ", result.toString());

    }

    @RabbitListener(queues = {"huida.ecu"})
    public void listenerHuidaEcuMessage(String message){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/mq_huida_ecu", params);
        logger.info("huida.ecu callback result:{} ", result.toString());
    }
    @RabbitListener(queues = {"huida.workarea"})
    public void listenerHuidaWorkareaMessage(String message){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/mq_huida_workarea", params);
        logger.info("huida.workarea callback result:{} ", result.toString());
    }
    @RabbitListener(queues = {"huida.onoff"})
    public void listenerHuidaOnoffMessage(String message){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/mq_huida_onoff", params);
        logger.info("huida.onoff callback result:{} ", result.toString());
    }
    @RabbitListener(queues = {"huida.terminal"})
    public void listenerHuidaTerminalMessage(String message){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/mq_huida_terminal", params);
        logger.info("huida.terminal callback result:{} ", result.toString());
    }

    //苏农云
    @RabbitListener(queues = {"sunong.common"})
    public void listenerSunongCommonMessage(String message){
        Map<String, Object> params = new HashMap<>();
        params.put("message",message);
        Object result = service.execute("POST", "/interface/callback/sunongCommon", params);
        logger.info("sunong.common callback result:{} ", result.toString());
    }
}
